﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Configuration;

namespace BHair.Base
{
    public partial class frmsk : Form
    {
        public frmsk()
        {
            InitializeComponent();
        }
        string siweils = "";
        string constr = ConfigurationManager.ConnectionStrings["BHair.Properties.Settings.BHairDBConnectionString"].ConnectionString;
        private void frmsk_Load(object sender, EventArgs e)
        {
            skfs();
            djh();

            txtys.Text = chuanzhi.fkje.ToString();
            this.txtss.Focus();
        }
        /// <summary>
        /// 加载收款方式
        /// </summary>
        public void skfs()
        {
            SqlConnection conn = new SqlConnection(constr);
            try
            {
                conn.Open();
                string sql = "SELECT [zhID],[zhname] FROM [BHairDB].[dbo].[yhzh]";
                SqlDataAdapter sda = new SqlDataAdapter(sql, conn);
                DataSet ds = new DataSet();
                sda.Fill(ds, "yhzh");
                comboBox1.DataSource = ds.Tables["yhzh"];
                comboBox1.DisplayMember = "zhname";
            }
            catch (Exception ee)
            {
                conn.Close();
                MessageBox.Show(ee.Message);
            }

        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                if (decimal.Parse(txtys.Text) <= decimal.Parse(txtss.Text) + decimal.Parse(txtyh.Text))
                {

                    insertshourumingxi();
                    chuanzhi.result = 1;
                    this.Close();

                }
                else
                {
                    MessageBox.Show("请填写正确的办卡收款金额");
                }
            }
            catch { MessageBox.Show("请正确填写收款金额"); }
        }
        public void insertshourumingxi()
        {
            string sql =string.Format( "INSERT INTO 收入明细 (djh, skje, zsje, djje, yhje, skfs, djbz, jsr, djrq, 班组) VALUES ('{0}','{1}','0','{2}','{3}','{4}','{5}','{6}','{7}','{8}')",textBox1.Text,txtss.Text,txtys.Text,txtyh.Text,comboBox1.Text,"会员办卡卡号:"+chuanzhi.bkcardid+"姓名："+chuanzhi.bankaname,chuanzhi.userID, DateTime.Now.ToString("yyyy-MM-dd"), chuanzhi.banzu);
            SqlConnection conn = new SqlConnection(constr);
            conn.Open();
            SqlCommand comm = new SqlCommand(sql,conn);
            comm.ExecuteNonQuery();
            djhshuaxin();
            MessageBox.Show("收款成功！");
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void txtyh_TextChanged(object sender, EventArgs e)
        {
            try
            {
                txtss.Text = (decimal.Parse(txtys.Text) - decimal.Parse(txtyh.Text)).ToString();
            }
            catch { }
        }

        private void txtss_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                txtyh.Text = (decimal.Parse(txtys.Text) - decimal.Parse(txtss.Text)).ToString();
            }
        }
        public void djh()
        {
            string lsh = "";
            try
            {
                SqlConnection conn = new SqlConnection(constr);
                conn.Open();
                string sql = "SELECT [bk] FROM [BHairDB].[dbo].[djh]";
                SqlCommand comm = new SqlCommand(sql, conn);
                SqlDataReader sdr = comm.ExecuteReader();
                while (sdr.Read())
                {
                    lsh = sdr["bk"].ToString();
                }
                string bs = "BK-";
                string time = DateTime.Now.ToString("yyyy-MM-dd");
                /*
                Random ran = new Random();
                int RandKey = ran.Next(10, 999);
                */
                int djlsh = Int32.Parse(lsh) + 1;
                siweils = string.Format("{0:00000}", djlsh);
                string djh = bs + time + "-" + chuanzhi.banzu + siweils;
                this.textBox1.Text = djh;
              
               // dgv.Rows.Clear();
            }
            catch (Exception ee)
            {
                MessageBox.Show(ee.Message);
            }

        }
        public void djhshuaxin()
        {
            try
            {
                SqlConnection conn = new SqlConnection(constr);
                conn.Open();

                string sql = string.Format("UPDATE [BHairDB].[dbo].[djh] SET [bk] = '{0}'", siweils);
                SqlCommand comm = new SqlCommand(sql, conn);
                comm.ExecuteNonQuery();
            }
            catch { }
        }
    }
}
